package cn.edu.scau.cmi.xuminbang.client.hibernate;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder.Case;

import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.xuminbang.hibernate.dao.MajorDAO;
import cn.edu.scau.cmi.xuminbang.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.xuminbang.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.xuminbang.hibernate.domain.Major;
import cn.edu.scau.cmi.xuminbang.hibernate.domain.Student;
import cn.edu.scau.cmi.xuminbang.hibernate.domain.Teacher;
import cn.edu.scau.cmi.xuminbang.util.ApplicationContextUtil;

public class HibernateClientHibernateDao {

	public static void main(String[] args) {
		
		show();
	}
	public static void show() {
		long stu_id = 000;String stu_name = null;long stu_num = 000;
		long stu_major = 000;long stu_tutor = 000;
		long teacher_id = 000;String teacher_name=null;
		System.out.println("选择你需要的功能：");
		System.out.println("功能1：添加学生\r\n" + 
				"功能2：修改学生\r\n" + 
				"功能3：删除学生\r\n" + 
				"功能4：添加教师\r\n" + 
				"功能5：修改教师\r\n" + 
				"功能6：删除教师\r\n" + 
				"功能7：学生选择指导教师\r\n" + 
				"功能8：学生修改指导教师\r\n" + 
				"功能9：教师选择被指导的学生\r\n" + 
				"功能10：教师修改被指导的学生\r\n" 
				+"功能0：退出\r\n");
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		switch(a) {
		case 0 :
			return;
		case 1 :
			System.out.println("请输入学生ID");
			stu_id = scanner.nextInt();
			System.out.println("请输入学生姓名");
			stu_name = scanner.next();
			System.out.println("请输入学生学号");
			stu_num = scanner.nextInt();
			//System.out.println("请输入学生的专业的编号");
			//stu_major = scanner.nextInt();
			//Major major = new Major();
			//major.setId(stu_major);
			Student student = new Student();
			student.setId(stu_id);
			student.setName(stu_name);
			student.setNumber(stu_num);
			///student.setMajor(major);
			newStudent(student);
			System.out.println("插入成功");
			show();
			break;
		case 2 :
			System.out.println("请输入学生ID");
			stu_id = scanner.nextInt();
			System.out.println("请输入学生姓名");
			stu_name = scanner.next();
			System.out.println("请输入学生学号");
			stu_num = scanner.nextInt();
			//System.out.println("请输入学生的专业的编号");
			//stu_major = scanner.nextInt();
			//major.setId(stu_major);
			//System.out.println("请输入学生的导师编号");
			//stu_tutor = scanner.nextInt();
			//Teacher teacher = new Teacher();
			//teacher.setId(stu_tutor);
			Student student2 = new Student();
			student2.setId(stu_id);
			student2.setName(stu_name);
			student2.setNumber(stu_num);
			//student2.setMajor(major);
			//student2.setTeacher(teacher);
			upDateStudent(student2);
			System.out.println("修改成功");
			show();
			break;
		case 3 ://删除
			System.out.println("请输入学生ID");
			stu_id = scanner.nextInt();
			Student student1 = null;
			StudentDAO studentDAO1 = new StudentDAO();//找出学生;
			Student studentFound = studentDAO1.findById(stu_id);
			student1 = studentFound;
			deleteStudent(student1);
			System.out.println("删除成功");
			show();
			break;
		case 4 :
			System.out.println("请输入老师ID");
			teacher_id = scanner.nextInt();
			System.out.println("请输入老师姓名");
			teacher_name = scanner.next();
			Teacher teacher = new Teacher(teacher_id);
			teacher.setName(teacher_name);
			newTeacher(teacher);
			System.out.println("插入成功");
			show();
			break;
		case 5 :
			System.out.println("请输入老师ID");
			teacher_id = scanner.nextInt();
			System.out.println("请输入老师姓名");
			teacher_name = scanner.next();
			Teacher teacher1 = new Teacher(teacher_id);
			teacher1.setName(teacher_name);
			upDateTeacher(teacher1);
			System.out.println("修改成功");
			show();
			break;
		case 6 :
			System.out.println("请输入老师ID");
			teacher_id = scanner.nextInt();
			TeacherDAO teacherDAO1 = new TeacherDAO();//找出老师;
			Teacher teacherFound = teacherDAO1.findById(teacher_id);
			deleteTeacher(teacherFound);
			System.out.println("删除成功");
			show();
			break;
		case 7 ://学生选择老师
			System.out.println("请输入学生ID");
			stu_id = scanner.nextInt();
			System.out.println("请输入选择的老师ID");
			teacher_id = scanner.nextInt();
			StudentDAO studentDAO2 = new StudentDAO();//找出学生;
			Student studentFound1 =null;		
			studentFound1 = studentDAO2.findById(stu_id);
			TeacherDAO teacherDAO2 = new TeacherDAO();//找出老师;
			Teacher teacherFound1 = null;
			teacherFound1 = teacherDAO2.findById(teacher_id);			
			Student student3 = new Student();
			student3.setId(stu_id);
			student3.setName(studentFound1.getName());
			student3.setNumber(studentFound1.getNumber());
			student3.setTeacher(teacherFound1);
			upDateStudent(student3);
			System.out.println("选择成功");
			show();
			break;
		case 8 ://学生修改老师
			System.out.println("请输入学生ID");
			stu_id = scanner.nextInt();
			System.out.println("请输入修改为的老师ID");
			teacher_id = scanner.nextInt();
			StudentDAO studentDAO3 = new StudentDAO();//找出学生;
			Student studentFound3 =null;		
			studentFound1 = studentDAO3.findById(stu_id);
			TeacherDAO teacherDAO3 = new TeacherDAO();//找出老师;
			Teacher teacherFound3 = null;
			teacherFound1 = teacherDAO3.findById(teacher_id);			
			Student student4 = new Student();
			student4.setId(stu_id);
			student4.setName(studentFound1.getName());
			student4.setNumber(studentFound1.getNumber());
			student4.setTeacher(teacherFound1);
			upDateStudent(student4);
			System.out.println("修改成功");
			show();
			break;
		case 9 :
			System.out.println("请输入老师ID");
			teacher_id = scanner.nextLong();
			TeacherDAO teacherDAO5 = new TeacherDAO();//找出老师;
			Teacher teacherFound4 = null;
			teacherFound4 = teacherDAO5.findById(teacher_id);
			System.out.println("请输入要选择的学生的ID");
			stu_id = scanner.nextLong();
			StudentDAO studentDAO4 = new StudentDAO();//找出学生;
			Student studentFound4 =null;		
			studentFound4 = studentDAO4.findById(stu_id);
			Student student5 = new Student();
			student5.setId(stu_id);
			student5.setName(studentFound4.getName());
			student5.setNumber(studentFound4.getNumber());
			student5.setTeacher(teacherFound4);
			upDateStudent(student5);
			System.out.println("选择成功");
			break;
		case 10 :
			System.out.println("请输入老师ID");
			teacher_id = scanner.nextLong();
			TeacherDAO teacherDAO4 = new TeacherDAO();//找出老师;
			Teacher teacherFound2 = null;
			teacherFound2 = teacherDAO4.findById(teacher_id);
			Set<Student> set = new HashSet(0);
			set = teacherFound2.getStudents();
			Iterator<Student> iterator = set.iterator();
			if(set.size()==0) {
				System.out.println("当前老师没有学生");
			}
			else {
				while(iterator.hasNext()){
					Student student0 = iterator.next();
					System.out.println(student0.getId()+"\t"+student0.getName());
					}
			}
			System.out.println("请输入需要修改的学生ID");
			stu_id = scanner.nextLong();
			//从哈希集合中移除，并更新学生的表
			while(iterator.hasNext()){
				Student student0 = iterator.next();
				if(student0.getId()==stu_id) {
					set.remove(student0);
				}
				}
			Student student6 = new Student();
			Student studentFound5 =null;
			StudentDAO studentDAO5 = new StudentDAO();
			studentFound5 = studentDAO5.findById(stu_id);
			student6.setId(stu_id);
			student6.setName(studentFound5.getName());
			student6.setNumber(studentFound5.getNumber());
			student6.setTeacher(null);
			upDateStudent(student6);
			System.out.println("修改成功");

			show();
		}
	}
	public static void upDateTeacher(Teacher teacher) {
		TeacherDAO teacherDAO = new TeacherDAO();
		
		Transaction transaction = teacherDAO.getSession().beginTransaction();
		
		teacherDAO.merge(teacher);
		transaction.commit();

		teacherDAO.getSession().flush();
	}
	public static void upDateStudent(Student student) {
		StudentDAO studentDAO = new StudentDAO();
		
		Transaction transaction = studentDAO.getSession().beginTransaction();
		
		studentDAO.merge(student);
		transaction.commit();

		studentDAO.getSession().flush();
	}
	
	public static void deleteStudent(Student student) {
		StudentDAO studentDAO = new StudentDAO();
		
		Transaction transaction = studentDAO.getSession().beginTransaction();
		
		studentDAO.delete(student);
		transaction.commit();

		studentDAO.getSession().flush();
	}
	public static void deleteTeacher(Teacher teacher) {
		TeacherDAO teacherDAO = new TeacherDAO();
		
		Transaction transaction = teacherDAO.getSession().beginTransaction();
		
		teacherDAO.delete(teacher);
		transaction.commit();

		teacherDAO.getSession().flush();
	}
	public static void newTeacher(Teacher teacher) {
		TeacherDAO teacherDAO = new TeacherDAO();
		
		Transaction transaction = teacherDAO.getSession().beginTransaction();
		
		teacherDAO.save(teacher);
		transaction.commit();

		teacherDAO.getSession().flush();
	}
	public static void newStudent(Student student) {
		ApplicationContext applicationContext=ApplicationContextUtil.getApplicationContext();
		StudentDAO studentDAO=(StudentDAO) applicationContext.getBean("studentDAO");
		
		Transaction transaction = studentDAO.getSession().beginTransaction();
		
		studentDAO.save(student);
		transaction.commit();
		studentDAO.getSession().flush();
	}
		public static void newMajor(Major major) {
			MajorDAO majorDAO=new MajorDAO();
			
			Transaction transaction = majorDAO.getSession().beginTransaction();
			
			majorDAO.save(major);
			transaction.commit();
		}

		private static void listMajors() {
			MajorDAO MajorDAO=new MajorDAO();
			List Majors = MajorDAO.findAll();
			Iterator<?> MajorIterator = Majors.iterator();
			while(MajorIterator.hasNext()){
				Major Major = (Major) MajorIterator.next();
				System.out.println("调用DAO后的专业名是："+Major.getName()+"，调用DAO后的专业ID号是："+Major.getId());
			}
		}
		private static void listStudents() {
			StudentDAO studentDAO = new StudentDAO();
			List student = studentDAO.findAll();
			Iterator<?> StudentIterator = student.iterator();
			while(StudentIterator.hasNext()){
				Student Student = (Student) StudentIterator.next();
				System.out.println("调用DAO后的学生名是："+Student.getName()+"，调用DAO后的学生ID号是："+Student.getId());
			}
		}

}
